package com.ruoyi.xiaoshouchuku.controller;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.ruoyi.kucunpandian.domain.Kucunpandian;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi;
import com.ruoyi.shangpinxinxi.domain.TestResult;
import com.ruoyi.kucunpandian.service.IKucunpandianService;
import com.ruoyi.shangpinxinxi.service.IShangpinxinxiService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.xiaoshouchuku.domain.XiaoshouChuku;
import com.ruoyi.xiaoshouchuku.service.IXiaoshouChukuService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 销售出库Controller
 *
 * @author ruoyi
 * @date 2023-05-15
 */
@Controller
@RequestMapping("/xiaoshouchuku/chukuTest")
public class XiaoshouChukuController extends BaseController {
    private String prefix = "xiaoshouchuku/chukuTest";

    @Autowired
    private IXiaoshouChukuService xiaoshouChukuService;

    @Autowired
    private IKucunpandianService kucunpandianService;

    @Autowired
    private IShangpinxinxiService shangpinxinxiService;

    @RequiresPermissions("xiaoshouchuku:chukuTest:view")
    @GetMapping()
    public String chukuTest() {
        return prefix + "/chukuTest";
    }

    public XiaoshouChuku xiaoshouChuku() {
        return xiaoshouChukuService.selectXiaoshouChukuById(1L);
    }

    /**
     * 查询销售出库列表
     */
    @RequiresPermissions("xiaoshouchuku:chukuTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(XiaoshouChuku xiaoshouChuku) {
        startPage();
        List<XiaoshouChuku> list = xiaoshouChukuService.selectXiaoshouChukuList(xiaoshouChuku);
        return getDataTable(list);
    }

    /**
     * 导出销售出库列表
     */
    @RequiresPermissions("xiaoshouchuku:chukuTest:export")
    @Log(title = "销售出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(XiaoshouChuku xiaoshouChuku) {
        List<XiaoshouChuku> list = xiaoshouChukuService.selectXiaoshouChukuList(xiaoshouChuku);
        ExcelUtil<XiaoshouChuku> util = new ExcelUtil<XiaoshouChuku>(XiaoshouChuku.class);
        return util.exportExcel(list, "销售出库数据");
    }

    /**
     * 新增销售出库
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存销售出库
     */
    @RequiresPermissions("xiaoshouchuku:chukuTest:add")
    @Log(title = "销售出库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(XiaoshouChuku xiaoshouChuku, HttpServletRequest request) {
        if (!xiaoshouChuku.getFahuoDanhao().contains("MDQ")) {
            return toAjax("发货单号填写不正确");
        }
        String danhaoNew = danhaoset();
        xiaoshouChuku.setChukuDanhao(danhaoNew);
        if (xiaoshouChuku.getShouhuoDizhi() == null || xiaoshouChuku.getShouhuoDizhi().equals("")) {
            return toAjax("未填写地址");
        }
        long wujin1 = 0, wujin2 = 0, wujin3 = 0, wujin1c, wujin2c, wujin3c, zongjine1, zongjine2;
        String wujin1str, wujin2str, wujin3str;
        switch (xiaoshouChuku.getPeitao1()) {
            case "整套门":
                TestResult zhengtaomen = new TestResult();
                zhengtaomen.setMenName(xiaoshouChuku.getGoodNameMen1());
                zhengtaomen.setMenKuan(xiaoshouChuku.getMen1shanKuan());
                zhengtaomen.setMenGao(xiaoshouChuku.getMen1shanGao());
                zhengtaomen.setKaiXiang(xiaoshouChuku.getKaiXiang1());
                zhengtaomen.setLeibie("%"+xiaoshouChuku.getPeitao1()+"%");
                List<Shangpinxinxi> zhengtaomenlist = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(zhengtaomen);
                Kucunpandian zhengtaomenk = kucunpandianService.selectKucunpandianByNameAndSize2(zhengtaomen);

                if (zhengtaomenlist.size() == 0) {
                    return toAjax("没有该整套门商品");
                }
                if (zhengtaomenk == null) {
                    return toAjax("没有该整套门商品的库存");
                }

                xiaoshouChuku.setTupian1(zhengtaomenlist.get(0).getTiaoma());

                wujinSet(xiaoshouChuku);
                wujin1str = wujin1Update(xiaoshouChuku);
                wujin2str = wujin2Update(xiaoshouChuku);
                wujin3str = wujin3Update(xiaoshouChuku);

                if (wujin1str != null) {
                    wujin1 = Long.parseLong(wujin1str.split("，")[0]);
                    wujin1c = Long.parseLong(wujin1str.split("，")[1]);
                } else {
                    wujin1 = 0;
                    wujin1c = 0;
                }
                if (wujin2str != null) {
                    wujin2 = Long.parseLong(wujin2str.split("，")[0]);
                    wujin2c = Long.parseLong(wujin2str.split("，")[1]);
                } else {
                    wujin2 = 0;
                    wujin2c = 0;
                }
                if (wujin3str != null) {
                    wujin3 = Long.parseLong(wujin3str.split("，")[0]);
                    wujin3c = Long.parseLong(wujin3str.split("，")[1]);
                } else {
                    wujin3 = 0;
                    wujin3c = 0;
                }

                if (xiaoshouChuku.getZhonjine() == null || xiaoshouChuku.getZhonjine() == 0) {
                    zongjine1 = Long.parseLong(xiaoshouChuku.getMen1Shuliang())
                            * Long.parseLong(zhengtaomenlist.get(0).getShangpinShoujia())
                            + wujin1 + wujin2 + wujin3;
                    xiaoshouChuku.setZhonjine(zongjine1);
                }
                zongjine2 = Long.parseLong(xiaoshouChuku.getMen1Shuliang())
                        * Long.parseLong(zhengtaomenlist.get(0).getJinhuoJia())
                        + wujin1c + wujin2c + wujin3c;
                xiaoshouChuku.setChengbenjine(zongjine2);

                xiaoshouChuku.setGoodNameMen2(null);
                xiaoshouChuku.setMen2shanKuan(null);
                xiaoshouChuku.setMen2shanGao(null);
                xiaoshouChuku.setKaiXiang2(null);
                xiaoshouChuku.setMen2Shuliang(null);

                xiaoshouChuku.setKuangShuliang(null);
                xiaoshouChuku.setGoodNameKuang(null);
                xiaoshouChuku.setKuangKuan(null);
                xiaoshouChuku.setKuangGao(null);
                xiaoshouChuku.setKaiXiang3(null);

                kucunpandianService.updateKucunpandian(zhengtaomenk);

                break;
            case "门扇门框配套":
                TestResult zuoshanmen = new TestResult();
                zuoshanmen.setMenName(xiaoshouChuku.getGoodNameMen1());
                zuoshanmen.setMenKuan(xiaoshouChuku.getMen1shanKuan());
                zuoshanmen.setMenGao(xiaoshouChuku.getMen1shanGao());
                zuoshanmen.setKaiXiang(xiaoshouChuku.getKaiXiang1());
                if (xiaoshouChuku.getMen2Shuliang().hashCode() != 0) {
                    zuoshanmen.setLeibie("%左扇%");
                }else {
                    zuoshanmen.setLeibie("%门扇%");
                }
                List<Shangpinxinxi> zuoshanmens = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(zuoshanmen);
                Kucunpandian zuoshanmenk = kucunpandianService.selectKucunpandianByNameAndSize2(zuoshanmen);

                if (zuoshanmens.size() == 0) {
                    return toAjax("没有该左门扇商品");
                }
                if (zuoshanmenk == null) {
                    return toAjax("没有该左门扇商品的库存");
                }
                xiaoshouChuku.setTupian1(zuoshanmens.get(0).getTiaoma());
                List<Shangpinxinxi> youshanmens = new ArrayList<>();Kucunpandian youshanmenk = new Kucunpandian();
                if (xiaoshouChuku.getGoodNameMen2().hashCode() != 0) {
                    TestResult yousahnmen = new TestResult();
                    yousahnmen.setMenName(xiaoshouChuku.getGoodNameMen2());
                    yousahnmen.setMenKuan(xiaoshouChuku.getMen2shanKuan());
                    yousahnmen.setMenGao(xiaoshouChuku.getMen2shanGao());
                    yousahnmen.setKaiXiang(xiaoshouChuku.getKaiXiang2());
                    yousahnmen.setLeibie("%右扇%");
                    youshanmens = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(yousahnmen);
                    youshanmenk = kucunpandianService.selectKucunpandianByNameAndSize2(yousahnmen);

                    if (youshanmens.size() == 0) {
                        return toAjax("没有该右门扇商品");
                    }
                    if (youshanmenk == null) {
                        return toAjax("没有该右门扇商品的库存");
                    }

                    xiaoshouChuku.setTupian2(youshanmens.get(0).getTiaoma());

                    if (Long.parseLong(xiaoshouChuku.getMen2shanKuan()) != Long.parseLong(xiaoshouChuku.getKuangKuan())
                            || Long.parseLong(xiaoshouChuku.getMen2shanGao()) != Long.parseLong(xiaoshouChuku.getKuangGao())) {
                        return toAjax("门扇门框的宽或高不匹配");
                    }

                    long yousahnshu = Long.parseLong(xiaoshouChuku.getMen2Shuliang());
                    if (youshanmenk.getKuncun()-yousahnshu < 0) {
                        return toAjax("左扇门商品库存数量不足");
                    }
                    youshanmenk.setKuncun(youshanmenk.getKuncun()-yousahnshu);

                    kucunpandianService.updateKucunpandian(youshanmenk);
                }

                if (xiaoshouChuku.getGoodNameKuang().hashCode() == 0) {
                    return toAjax("没有选中门框进行配套");
                }

                if (Long.parseLong(xiaoshouChuku.getMen1shanKuan()) != Long.parseLong(xiaoshouChuku.getKuangKuan())
                || Long.parseLong(xiaoshouChuku.getMen1shanGao()) != Long.parseLong(xiaoshouChuku.getKuangGao())) {
                  return toAjax("门扇门框的宽或高不匹配");
                }

                long zuoshu = Long.parseLong(xiaoshouChuku.getMen1Shuliang());
                if (zuoshanmenk.getKuncun()-zuoshu <0) {
                    return toAjax("左扇门商品库存数量不足");
                }
                zuoshanmenk.setKuncun(zuoshanmenk.getKuncun()-zuoshu);

                kucunpandianService.updateKucunpandian(zuoshanmenk);

                TestResult kuang = new TestResult();
                kuang.setMenName(xiaoshouChuku.getGoodNameKuang());
                kuang.setLeibie("%双开%门框%");
                kuang.setMenKuan(xiaoshouChuku.getKuangKuan());
                kuang.setMenGao(xiaoshouChuku.getKuangGao());
                kuang.setKaiXiang(xiaoshouChuku.getKaiXiang3());
                List<Shangpinxinxi> kuangs = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(kuang);
                Kucunpandian kuangk = kucunpandianService.selectKucunpandianByNameAndSize2(kuang);

                if (kuangs.size() == 0) {
                    return toAjax("没有该门框商品");
                }
                if (kuangk == null) {
                    return toAjax("没有该门框商品的库存");
                }

                xiaoshouChuku.setTupian3(kuangs.get(0).getTiaoma());

                long kuangshu = Long.parseLong(xiaoshouChuku.getKuangShuliang());
                if (kuangk.getKuncun()-kuangshu <0) {
                    return toAjax("左扇门商品库存数量不足");
                }
                zuoshanmenk.setKuncun(kuangk.getKuncun()-kuangshu);

                kucunpandianService.updateKucunpandian(kuangk);

                wujinSet(xiaoshouChuku);
                wujin1str = wujin1Update(xiaoshouChuku);
                wujin2str = wujin2Update(xiaoshouChuku);
                wujin3str = wujin3Update(xiaoshouChuku);

                if (wujin1str != null) {
                    wujin1 = Long.parseLong(wujin1str.split("，")[0]);
                    wujin1c = Long.parseLong(wujin1str.split("，")[1]);
                } else {
                    wujin1 = 0;
                    wujin1c = 0;
                }
                if (wujin2str != null) {
                    wujin2 = Long.parseLong(wujin2str.split("，")[0]);
                    wujin2c = Long.parseLong(wujin2str.split("，")[1]);
                } else {
                    wujin2 = 0;
                    wujin2c = 0;
                }
                if (wujin3str != null) {
                    wujin3 = Long.parseLong(wujin3str.split("，")[0]);
                    wujin3c = Long.parseLong(wujin3str.split("，")[1]);
                } else {
                    wujin3 = 0;
                    wujin3c = 0;
                }
                if (xiaoshouChuku.getZhonjine() == null) {
                    if (youshanmens.size() != 0) {
                        zongjine1 = (Long.parseLong(zuoshanmens.get(0).getShangpinShoujia())*Long.parseLong(xiaoshouChuku.getMen1Shuliang()))
                        +(Long.parseLong(youshanmens.get(0).getShangpinShoujia())*Long.parseLong(xiaoshouChuku.getMen2Shuliang()))
                        +wujin1+wujin2+wujin3;
                    }else {
                        zongjine1 = (Long.parseLong(zuoshanmens.get(0).getShangpinShoujia())*Long.parseLong(xiaoshouChuku.getMen1Shuliang()))
                                +wujin1+wujin2+wujin3;
                    }
                    xiaoshouChuku.setZhonjine(zongjine1);
                }
                if (youshanmens.size() != 0) {
                    zongjine2 = (Long.parseLong(zuoshanmens.get(0).getShangpinShoujia())*Long.parseLong(xiaoshouChuku.getMen1Shuliang()))
                            +(Long.parseLong(youshanmens.get(0).getShangpinShoujia())*Long.parseLong(xiaoshouChuku.getMen2Shuliang()))
                            +wujin1c+wujin2c+wujin3c;
                }else {
                    zongjine2 = (Long.parseLong(zuoshanmens.get(0).getShangpinShoujia())*Long.parseLong(xiaoshouChuku.getMen1Shuliang()))
                            +wujin1c+wujin2c+wujin3c;
                }
                xiaoshouChuku.setChengbenjine(zongjine2);
                break;
            case "单订门扇":
                TestResult danmenshan = new TestResult();
                danmenshan.setMenName(xiaoshouChuku.getGoodNameMen1());
                danmenshan.setMenKuan(xiaoshouChuku.getMen1shanKuan());
                danmenshan.setMenGao(xiaoshouChuku.getMen1shanGao());
                danmenshan.setKaiXiang(xiaoshouChuku.getKaiXiang1());
                danmenshan.setLeibie("%门扇%");
                List<Shangpinxinxi> danmenshans = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(danmenshan);
                Kucunpandian danmenshank = kucunpandianService.selectKucunpandianByNameAndSize2(danmenshan);

                if (danmenshans.size() == 0) {
                    return toAjax("没有该门扇商品");
                }
                if (danmenshank == null) {
                    return toAjax("没有该门扇商品的库存");
                }

                long danmennew = Long.parseLong(xiaoshouChuku.getMen1Shuliang());
                if (danmenshank.getKuncun()-danmennew <0) {
                    return toAjax("该门扇商品库存数量不足");
                }
                danmenshank.setKuncun(danmenshank.getKuncun()-danmennew);
                kucunpandianService.updateKucunpandian(danmenshank);

                xiaoshouChuku.setTupian1(danmenshans.get(0).getTiaoma());

                wujinSet(xiaoshouChuku);
                wujin1str = wujin1Update(xiaoshouChuku);
                wujin2str = wujin2Update(xiaoshouChuku);
                wujin3str = wujin3Update(xiaoshouChuku);

                if (wujin1str != null) {
                    wujin1 = Long.parseLong(wujin1str.split("，")[0]);
                    wujin1c = Long.parseLong(wujin1str.split("，")[1]);
                } else {
                    wujin1 = 0;
                    wujin1c = 0;
                }
                if (wujin2str != null) {
                    wujin2 = Long.parseLong(wujin2str.split("，")[0]);
                    wujin2c = Long.parseLong(wujin2str.split("，")[1]);
                } else {
                    wujin2 = 0;
                    wujin2c = 0;
                }
                if (wujin3str != null) {
                    wujin3 = Long.parseLong(wujin3str.split("，")[0]);
                    wujin3c = Long.parseLong(wujin3str.split("，")[1]);
                } else {
                    wujin3 = 0;
                    wujin3c = 0;
                }

                if (xiaoshouChuku.getZhonjine() == null) {
                    zongjine1 = (Long.parseLong(danmenshans.get(0).getShangpinShoujia())*Long.parseLong(xiaoshouChuku.getMen1Shuliang()))
                                +wujin1+wujin2+wujin3;
                    xiaoshouChuku.setZhonjine(zongjine1);
                }
                zongjine2 = (Long.parseLong(danmenshans.get(0).getShangpinShoujia())*Long.parseLong(xiaoshouChuku.getMen1Shuliang()))
                            +wujin1c+wujin2c+wujin3c;
                xiaoshouChuku.setChengbenjine(zongjine2);

                xiaoshouChuku.setGoodNameMen2(null);
                xiaoshouChuku.setMen2shanKuan(null);
                xiaoshouChuku.setMen2shanGao(null);
                xiaoshouChuku.setKaiXiang2(null);
                xiaoshouChuku.setMen2Shuliang(null);

                xiaoshouChuku.setKuangShuliang(null);
                xiaoshouChuku.setGoodNameKuang(null);
                xiaoshouChuku.setKuangKuan(null);
                xiaoshouChuku.setKuangGao(null);
                xiaoshouChuku.setKaiXiang3(null);
                break;
            case "单订门框":
                TestResult danmenkuang = new TestResult();
                danmenkuang.setMenName(xiaoshouChuku.getGoodNameKuang());
                danmenkuang.setMenKuan(xiaoshouChuku.getKuangKuan());
                danmenkuang.setMenGao(xiaoshouChuku.getKuangGao());
                danmenkuang.setKaiXiang(xiaoshouChuku.getKaiXiang3());
                danmenkuang.setLeibie("%门框%");
                List<Shangpinxinxi> danmenkuangs = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(danmenkuang);
                Kucunpandian danmenkuangk = kucunpandianService.selectKucunpandianByNameAndSize2(danmenkuang);

                if (danmenkuangs.size() == 0) {
                    return toAjax("没有该门框商品");
                }
                if (danmenkuangk == null) {
                    return toAjax("没有该门框商品的库存");
                }

                long dankuangnew = Long.parseLong(xiaoshouChuku.getKuangShuliang());
                if (danmenkuangk.getKuncun()-dankuangnew <0) {
                    return toAjax("该门扇商品库存数量不足");
                }
                danmenkuangk.setKuncun(danmenkuangk.getKuncun()-dankuangnew);
                kucunpandianService.updateKucunpandian(danmenkuangk);

                xiaoshouChuku.setTupian3(danmenkuangs.get(0).getTiaoma());

                wujinSet(xiaoshouChuku);
                wujin1str = wujin1Update(xiaoshouChuku);
                wujin2str = wujin2Update(xiaoshouChuku);
                wujin3str = wujin3Update(xiaoshouChuku);

                if (wujin1str != null) {
                    wujin1 = Long.parseLong(wujin1str.split("，")[0]);
                    wujin1c = Long.parseLong(wujin1str.split("，")[1]);
                } else {
                    wujin1 = 0;
                    wujin1c = 0;
                }
                if (wujin2str != null) {
                    wujin2 = Long.parseLong(wujin2str.split("，")[0]);
                    wujin2c = Long.parseLong(wujin2str.split("，")[1]);
                } else {
                    wujin2 = 0;
                    wujin2c = 0;
                }
                if (wujin3str != null) {
                    wujin3 = Long.parseLong(wujin3str.split("，")[0]);
                    wujin3c = Long.parseLong(wujin3str.split("，")[1]);
                } else {
                    wujin3 = 0;
                    wujin3c = 0;
                }

                if (xiaoshouChuku.getZhonjine() == null) {
                    zongjine1 = (Long.parseLong(danmenkuangs.get(0).getShangpinShoujia())*Long.parseLong(xiaoshouChuku.getKuangShuliang()))
                            +wujin1+wujin2+wujin3;
                    xiaoshouChuku.setZhonjine(zongjine1);
                }
                zongjine2 = (Long.parseLong(danmenkuangs.get(0).getShangpinShoujia())*Long.parseLong(xiaoshouChuku.getKuangShuliang()))
                        +wujin1c+wujin2c+wujin3c;
                xiaoshouChuku.setChengbenjine(zongjine2);

                xiaoshouChuku.setGoodNameMen2(null);
                xiaoshouChuku.setMen2shanKuan(null);
                xiaoshouChuku.setMen2shanGao(null);
                xiaoshouChuku.setKaiXiang2(null);
                xiaoshouChuku.setMen2Shuliang(null);

                xiaoshouChuku.setGoodNameMen1(null);
                xiaoshouChuku.setMen1shanKuan(null);
                xiaoshouChuku.setMen1shanGao(null);
                xiaoshouChuku.setKaiXiang1(null);
                xiaoshouChuku.setMen1Shuliang(null);
                break;
            case "仅五金出入":
                String set = wujinSet(xiaoshouChuku);

                if (set.equals("正常配套")) {
                    wujin1 = Long.parseLong(xiaoshouChuku.getShuliang1());
                    wujin2 = Long.parseLong(xiaoshouChuku.getShuliang2());
                    wujin3 = Long.parseLong(xiaoshouChuku.getShuliang3());
                }
                if (set.equals("单要防火锁")) {
                    wujin1 = Long.parseLong(xiaoshouChuku.getShuliang1());
                    wujin2 = 0;
                    wujin3 = 0;
                }
                if (set.equals("单要闭门器")) {
                    wujin1 = 0;
                    wujin2 = Long.parseLong(xiaoshouChuku.getShuliang2());
                    wujin3 = 0;
                }
                if (set.equals("单要拉爆盖")) {
                    wujin1 = 0;
                    wujin2 = 0;
                    wujin3 = Long.parseLong(xiaoshouChuku.getShuliang3());
                }

                TestResult wujin1t = new TestResult();
                wujin1t.setMenName("%防火锁%");
                wujin1t.setLeibie("%防火锁%");
                List<Shangpinxinxi> wujin1s = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(wujin1t);
                Kucunpandian wujin1k = kucunpandianService.selectKucunpandianByNameAndSize(wujin1t);

                TestResult wujin2t = new TestResult();
                wujin2t.setMenName("%闭门器%");
                wujin2t.setLeibie("%闭门器%");
                List<Shangpinxinxi> wujin2s = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(wujin2t);
                Kucunpandian wujin2k = kucunpandianService.selectKucunpandianByNameAndSize(wujin2t);

                TestResult wujin3t = new TestResult();
                wujin3t.setMenName("%拉爆盖%");
                wujin3t.setLeibie("%拉爆盖%");
                List<Shangpinxinxi> wujin3s = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(wujin3t);
                Kucunpandian wujin3k = kucunpandianService.selectKucunpandianByNameAndSize(wujin3t);

                if (wujin1k == null) {
                    return toAjax("库存中没有防火锁");
                }
                if (wujin2k == null) {
                    return toAjax("库存中没有闭门器");
                }
                if (wujin3k == null) {
                    return toAjax("库存中没有拉爆盖");
                }

                long new2wujin1 = wujin1k.getKuncun()-wujin1;
                long new2wujin2 = wujin2k.getKuncun()-wujin2;
                long new2wujin3 = wujin3k.getKuncun()-wujin3;
                if (new2wujin1 < 0) {
                    return toAjax("库存中防火锁数量不足");
                }
                if (new2wujin2 < 0) {
                    return toAjax("库存中闭门器数量不足");
                }
                if (new2wujin3 < 0) {
                    return toAjax("库存中拉爆盖数量不足");
                }

                wujin1k.setKuncun(new2wujin1);
                wujin2k.setKuncun(new2wujin2);
                wujin3k.setKuncun(new2wujin3);

                xiaoshouChuku.setGoodNameMen1(null);
                xiaoshouChuku.setMen1shanKuan(null);
                xiaoshouChuku.setMen1shanGao(null);
                xiaoshouChuku.setKaiXiang1(null);
                xiaoshouChuku.setMen1Shuliang(null);

                xiaoshouChuku.setGoodNameMen2(null);
                xiaoshouChuku.setMen2shanKuan(null);
                xiaoshouChuku.setMen2shanGao(null);
                xiaoshouChuku.setKaiXiang2(null);
                xiaoshouChuku.setMen2Shuliang(null);

                xiaoshouChuku.setKuangShuliang(null);
                xiaoshouChuku.setGoodNameKuang(null);
                xiaoshouChuku.setKuangKuan(null);
                xiaoshouChuku.setKuangGao(null);
                xiaoshouChuku.setKaiXiang3(null);

                if (xiaoshouChuku.getZhonjine() == null) {
                    zongjine1 = (wujin1 * Long.parseLong(wujin1s.get(0).getShangpinShoujia()))
                            + (wujin2 * Long.parseLong(wujin2s.get(0).getShangpinShoujia()))
                            + (wujin3 * Long.parseLong(wujin3s.get(0).getShangpinShoujia()));
                    xiaoshouChuku.setZhonjine(zongjine1);
                }
                zongjine2 = (wujin1 * Long.parseLong(wujin1s.get(0).getJinhuoJia()))
                        + (wujin2 * Long.parseLong(wujin2s.get(0).getJinhuoJia()))
                        + (wujin3 * Long.parseLong(wujin3s.get(0).getJinhuoJia()));
                xiaoshouChuku.setZhonjine(zongjine2);
                kucunpandianService.updateKucunpandian(wujin1k);
                kucunpandianService.updateKucunpandian(wujin2k);
                kucunpandianService.updateKucunpandian(wujin3k);
                break;
            default:
                break;
        }
        xiaoshouChuku.setZhixing("未执行");
        return toAjax(xiaoshouChukuService.insertXiaoshouChuku(xiaoshouChuku));
    }

    private String wujinSet(XiaoshouChuku xiaoshouChuku) {
        String wujinset = "";
        switch (xiaoshouChuku.getPeitao2()) {
            case "常规五金配套":
                if (xiaoshouChuku.getShuliang1().hashCode() != 0 && xiaoshouChuku.getMen1Shuliang().hashCode() != 0) {
                    xiaoshouChuku.setShuliang1(xiaoshouChuku.getMen1Shuliang());
                    if (Double.parseDouble(xiaoshouChuku.getMen1shanKuan()) > 1000) {
                        xiaoshouChuku.setShuliang2(String.valueOf(Integer.parseInt(xiaoshouChuku.getMen1Shuliang()) * 2));
                    } else {
                        xiaoshouChuku.setShuliang2(xiaoshouChuku.getMen1Shuliang());
                    }
                    xiaoshouChuku.setShuliang3(String.valueOf(Integer.parseInt(xiaoshouChuku.getMen1Shuliang()) * 6));
                    wujinset = "正常配套";
                }
                if (xiaoshouChuku.getKuangShuliang().hashCode() != 0 && !xiaoshouChuku.getKuangShuliang().equals("")) {
                    xiaoshouChuku.setShuliang1(xiaoshouChuku.getKuangShuliang());
                    if (Double.parseDouble(xiaoshouChuku.getKuangKuan()) > 1000) {
                        xiaoshouChuku.setShuliang2(String.valueOf(Integer.parseInt(xiaoshouChuku.getKuangShuliang()) * 2));
                    } else {
                        xiaoshouChuku.setShuliang2(xiaoshouChuku.getKuangShuliang());
                    }
                    xiaoshouChuku.setShuliang3(String.valueOf(Integer.parseInt(xiaoshouChuku.getKuangShuliang()) * 6));
                    wujinset = "正常配套";
                }

                break;
            case "单要防火锁":
                xiaoshouChuku.setShuliang2(null);
                xiaoshouChuku.setShuliang3(null);
                wujinset = "防火锁";
                break;
            case "单要闭门器":
                xiaoshouChuku.setShuliang1(null);
                xiaoshouChuku.setShuliang3(null);
                wujinset = "闭门器";
                break;
            case "单要拉爆盖":
                xiaoshouChuku.setShuliang1(null);
                xiaoshouChuku.setShuliang2(null);
                wujinset = "拉爆盖";
                break;
            case "不含五金":
                xiaoshouChuku.setShuliang1(null);
                xiaoshouChuku.setShuliang2(null);
                xiaoshouChuku.setShuliang3(null);
                wujinset = "不含五金";
                break;
            default:
                break;
        }
        return wujinset;
    }

    private String wujin3Update(XiaoshouChuku xiaoshouChuku) {
        if (xiaoshouChuku.getShuliang3() != null) {
            TestResult wujin4t = new TestResult();
            wujin4t.setMenName("%拉爆盖%");
            wujin4t.setLeibie("%拉爆盖%");
            List<Shangpinxinxi> wujin = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(wujin4t);
            Kucunpandian wujin4 = kucunpandianService.selectKucunpandianByNameAndSize2(wujin4t);
            if (wujin4 == null) {
                return null;
            }
            if (wujin.size() == 0) {
                return null;
            }
            long newKuCunWuJin1 = wujin4.getKuncun() - Long.parseLong(xiaoshouChuku.getShuliang3());
            wujin4.setKuncun(newKuCunWuJin1);
            kucunpandianService.updateKucunpandian(wujin4);
            long zongjine = Long.parseLong(xiaoshouChuku.getShuliang3()) * Long.parseLong(wujin.get(0).getShangpinShoujia());
            long chengben = Long.parseLong(xiaoshouChuku.getShuliang3()) * Long.parseLong(wujin.get(0).getJinhuoJia());
            String a = zongjine + "，" + chengben;
            kucunpandianService.updateKucunpandian(wujin4);
            return a;
        }
        return null;
    }

    private String wujin2Update(XiaoshouChuku xiaoshouChuku) {
        if (xiaoshouChuku.getShuliang2() != null) {
            TestResult wujin4t = new TestResult();
            wujin4t.setMenName("%闭门器%");
            wujin4t.setLeibie("%闭门器%");
            List<Shangpinxinxi> wujin = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(wujin4t);
            Kucunpandian wujin4 = kucunpandianService.selectKucunpandianByNameAndSize2(wujin4t);
            if (wujin4 == null) {
                return null;
            }
            if (wujin.size() == 0) {
                return null;
            }
            long newKuCunWuJin1 = wujin4.getKuncun() - Long.parseLong(xiaoshouChuku.getShuliang2());
            wujin4.setKuncun(newKuCunWuJin1);
            long zongjine = Long.parseLong(xiaoshouChuku.getShuliang2()) * Long.parseLong(wujin.get(0).getShangpinShoujia());
            long chengben = Long.parseLong(xiaoshouChuku.getShuliang2()) * Long.parseLong(wujin.get(0).getJinhuoJia());
            String a = zongjine + "，" + chengben;
            kucunpandianService.updateKucunpandian(wujin4);
            return a;
        }
        return null;
    }

    private String wujin1Update(XiaoshouChuku xiaoshouChuku) {
        if (xiaoshouChuku.getShuliang1() != null) {
            TestResult wujin4t = new TestResult();
            wujin4t.setMenName("%防火锁%");
            wujin4t.setLeibie("%防火锁%");
            List<Shangpinxinxi> wujin = shangpinxinxiService.selectShangpinxinxiByNameAndSize2(wujin4t);
            Kucunpandian wujin4 = kucunpandianService.selectKucunpandianByNameAndSize2(wujin4t);
            if (wujin4 == null) {
                return null;
            }
            if (wujin.size() == 0) {
                return null;
            }
            long newKuCunWuJin1 = wujin4.getKuncun() - Long.parseLong(xiaoshouChuku.getShuliang1());
            wujin4.setKuncun(newKuCunWuJin1);
            long zongjine = Long.parseLong(xiaoshouChuku.getShuliang1()) * Long.parseLong(wujin.get(0).getShangpinShoujia());
            long chengben = Long.parseLong(xiaoshouChuku.getShuliang1()) * Long.parseLong(wujin.get(0).getJinhuoJia());
            String a = zongjine + "，" + chengben;
            kucunpandianService.updateKucunpandian(wujin4);
            return a;
        }
        return null;
    }

    private String danhaoset() {
        String danhaoStr = xiaoshouChukuService.selectGouhuoDanhaoByLastOne();
        if (danhaoStr == null) {
            return "00000001";
        }
        int num = Integer.parseInt(danhaoStr) + 1;
        String b = String.valueOf(num);
        if (b.length() < 8) {
            int a = 8 - b.length();
            for (int j = 0; j < a; j++) {
                b = "0" + b;
            }
        }
        return b;
    }

    /**
     * 修改销售出库
     */
    @RequiresPermissions("xiaoshouchuku:chukuTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        XiaoshouChuku xiaoshouChuku = xiaoshouChukuService.selectXiaoshouChukuById(id);
        mmap.put("xiaoshouChuku", xiaoshouChuku);
        return prefix + "/edit";
    }

    @PostMapping("/selectAll/{id}")
    @ResponseBody
    public AjaxResult selectALL(@PathVariable("id") Long id, HttpServletRequest request) {
        XiaoshouChuku xiaoshouChuku = xiaoshouChukuService.selectXiaoshouChukuById(id);
        request.getSession().setAttribute("xiaoshouChuku", xiaoshouChuku);
        return toAjax(1);
    }

    @GetMapping("/selectAll")
    public String selectAll() {
        return prefix + "/selectAll";
    }

    @GetMapping("/showOne/{id}")
    @ResponseBody
    public AjaxResult showone(@PathVariable("id") Long id, HttpServletRequest request) {
        XiaoshouChuku xiaoshouChuku = xiaoshouChukuService.selectXiaoshouChukuById(id);

        chuKuDanUpload.img2New(xiaoshouChuku);
        request.getSession().setAttribute("imgPath", "https://www.beilianjd.com:2560/profile/upload/ChuKuDan/" + xiaoshouChuku.getChukuDanhao() + ".jpg");
        return toAjax(1);
    }

    @GetMapping("/showOne")
    public String showOne() {
        return prefix + "/showOne";
    }

    /**
     * 修改保存销售出库
     */
    @RequiresPermissions("xiaoshouchuku:chukuTest:edit")
    @Log(title = "销售出库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(XiaoshouChuku xiaoshouChuku) throws IllegalAccessException {
        Field[] fields = xiaoshouChuku.getClass().getDeclaredFields();
        for (Field f : fields) {
            Object object = f.get(xiaoshouChuku);
            if (object.toString().equals("")) {
                f.setAccessible(true);
                f.set(xiaoshouChuku, null);
            }
        }
        return toAjax(xiaoshouChukuService.updateXiaoshouChuku(xiaoshouChuku));
    }

    /**
     * 删除销售出库
     */
    @RequiresPermissions("xiaoshouchuku:chukuTest:remove")
    @Log(title = "销售出库", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(xiaoshouChukuService.deleteXiaoshouChukuByIds(ids));
    }


    @PostMapping("/list2WeiZhiXing")
    @ResponseBody
    public TableDataInfo list2WeiZhiXing(XiaoshouChuku xiaoshouChuku, HttpServletRequest request) {
        startPage();
        xiaoshouChuku.setZhixing("未执行");
        String username = request.getSession().getAttribute("name4user").toString();
        xiaoshouChuku.setFahuoRen(username);
        List<XiaoshouChuku> list = xiaoshouChukuService.selectXiaoshouChukuList(xiaoshouChuku);
        return getDataTable(list);
    }
}
